package rabbitMQ.ack;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import rabbitMQ.utils.RabbitMqUtils;
import rabbitMQ.utils.SleepUtils;

/**
 * @program: rabbitMQ
 * @description:
 * @author: hjk
 * @create: 2021-07-07 16:09
 **/
public class Consumer {

    private static final String TASK_QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        System.out.println("等待消息消费。。。");
        DeliverCallback deliverCallback=(consumerTag, delivery)->{
            String receivedMessage = new String(delivery.getBody(),"UTF-8");
            SleepUtils.sleep(30);
            System.out.println("接收到消息:"+receivedMessage);
            /**
             * 1.消息标记 tag
             * 2.是否批量应答未应答消息
             */
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(),false);
        };
        //采用手动应答
        boolean ackAuto = false;
        channel.basicConsume(TASK_QUEUE_NAME,ackAuto,deliverCallback,consumerTag -> {});
    }
}
